package com.ruoyi.business.base.service.impl;

import com.ruoyi.business.base.domain.BaseSecondary;
import com.ruoyi.business.base.mapper.BaseSecondaryMapper;
import com.ruoyi.business.base.service.IBaseSecondaryService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 二级单位信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class BaseSecondaryServiceImpl implements IBaseSecondaryService
{
    @Autowired
    private BaseSecondaryMapper secondaryMapper;

    /**
     * 查询二级单位信息集合
     * 
     * @param secondary 二级单位信息
     * @return 二级单位信息集合
     */
    @Override
    public List<BaseSecondary> selectSecondaryList(BaseSecondary secondary)
    {
        return secondaryMapper.selectSecondaryList(secondary);
    }

    /**
     * 查询所有二级单位
     * 
     * @return 二级单位列表
     */
    @Override
    public List<BaseSecondary> selectSecondaryAll()
    {
        return secondaryMapper.selectSecondaryAll();
    }

    /**
     * 通过二级单位ID查询二级单位信息
     * 
     * @param secondaryId 二级单位ID
     * @return 角色对象信息
     */
    @Override
    public BaseSecondary selectSecondaryById(Long secondaryId)
    {
        return secondaryMapper.selectSecondaryById(secondaryId);
    }

    /**
     * 根据用户ID获取二级单位选择框列表
     * 
     * @param userId 用户ID
     * @return 选中二级单位ID列表
     */
    @Override
    public List<Long> selectSecondaryListByUserId(Long userId)
    {
        return secondaryMapper.selectSecondaryListByUserId(userId);
    }

    /**
     * 校验二级单位名称是否唯一
     * 
     * @param secondary 二级单位信息
     * @return 结果
     */
    @Override
    public boolean checkSecondaryNameUnique(BaseSecondary secondary)
    {
        Long secondaryId = StringUtils.isNull(secondary.getSecondaryId()) ? -1L : secondary.getSecondaryId();
        BaseSecondary info = secondaryMapper.checkSecondaryNameUnique(secondary.getSecondaryName());
        if (StringUtils.isNotNull(info) && info.getSecondaryId().longValue() != secondaryId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验二级单位编码是否唯一
     * 
     * @param secondary 二级单位信息
     * @return 结果
     */
    @Override
    public boolean checkSecondaryCodeUnique(BaseSecondary secondary)
    {
        Long secondaryId = StringUtils.isNull(secondary.getSecondaryId()) ? -1L : secondary.getSecondaryId();
        BaseSecondary info = secondaryMapper.checkSecondaryCodeUnique(secondary.getSecondaryCode());
        if (StringUtils.isNotNull(info) && info.getSecondaryId().longValue() != secondaryId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 删除二级单位信息
     * 
     * @param secondaryId 二级单位ID
     * @return 结果
     */
    @Override
    public int deleteSecondaryById(Long secondaryId)
    {
        return secondaryMapper.deleteSecondaryById(secondaryId);
    }

    /**
     * 批量删除二级单位信息
     * 
     * @param secondaryIds 需要删除的二级单位ID
     * @return 结果
     */
    @Override
    public int deleteSecondaryByIds(Long[] secondaryIds)
    {
        return secondaryMapper.deleteSecondaryByIds(secondaryIds);
    }

    /**
     * 新增保存二级单位信息
     * 
     * @param secondary 二级单位信息
     * @return 结果
     */
    @Override
    public int insertSecondary(BaseSecondary secondary)
    {
        return secondaryMapper.insertSecondary(secondary);
    }

    /**
     * 修改保存二级单位信息
     * 
     * @param secondary 二级单位信息
     * @return 结果
     */
    @Override
    public int updateSecondary(BaseSecondary secondary)
    {
        return secondaryMapper.updateSecondary(secondary);
    }
}
